home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 October / Macformat17.cdr / Shareware City / Developers / GAL ƒ / GAL examples / factorial < prev    next >
Encoding:
Text File  |  1994-07-11  |  484 b   |  23 lines  |  [TEXT/ttxt]

  1. ; factorial - the hard way
  2.  
  3. ; if r0 = 1 then fact = 1 else fact := r0*r1
  4.         begin_code
  5.         copy         #5=>r0            ; calculate fact (5)
  6.         jump:subroutine    ->fact
  7.         halt
  8.  
  9. fact
  10.         compare     #1=>r0                ; base case?
  11.         jump:less_than    generalcase
  12.         copy        #1=>r0
  13.         copy        #1=>r1                ; r1 serves as the multiplier
  14.         jump        endit
  15. generalcase
  16.         subtract    #1=>r0                ; n := n -1
  17.         jump:subroutine    ->fact    ; fact (n-1)
  18.         add        #1=>r1                ; get n back
  19.         multiply    r1=>r0                ; n * fact(n-1)
  20. endit
  21.         return
  22.         end_code
  23.         end